//
//  HomeViewController.m
//  TableBarControllerDemo
//
//  Created by 边果互动 on 2021/3/15.
//

#import "HomeViewController.h"
#import "BannerView.h"
#import "DynamicViewCell.h"
#import "MarketViewCell.h"
#import "ReleaseViewCell.h"
#import "GroupBean.h"
#import "PurchaseViewCell.h"
#import <AFNetworking/AFNetworking.h>
#import "AFNetworkHelper+Api.h"
#import "SourceBean.h"
#import "SourceDetailViewController.h"
#import "TextUtil.h"
#import "PhoneUtil.h"
#import <MJRefresh/MJRefresh.h>
#import "FormatUtil.h"

#define cell_id_dynamic @"DynamicViewCell"
#define cell_id_market @"MarketViewCell"
#define cell_id_release @"ReleaseViewCell"
#define cell_id_purchase @"PurchaseViewCell"

@interface HomeViewController ()

@end

@implementation HomeViewController

- (void)viewDidDisappear:(BOOL)animated {
    [((BannerView *)
      self.tableView.tableHeaderView) stopTimer];
}

- (void)viewDidAppear:(BOOL)animated {
    [((BannerView *)
      self.tableView.tableHeaderView) startTimer];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSearchTitle];
    NSMutableArray *list = [NSMutableArray array];
    [list addObject:@"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3571529949,229356399&fm=26&gp=0.jpg"];
    [list addObject:@"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2071531164,3511861342&fm=26&gp=0.jpg"];
    [list addObject:@"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3330665574,4010079980&fm=11&gp=0.jpg"];
    [list addObject:@"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2036390322,794403834&fm=26&gp=0.jpg"];
    [list addObject:@"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1902934933,556456904&fm=26&gp=0.jpg"];
    
    BannerView *bannerView = [BannerView bannerWithUrls:list];
    
    self.tableView.tableHeaderView = bannerView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //设置偏移量，解决滑动到底部回弹被遮挡问题
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, TabBarHeight, 0);
    MJRefreshHeader *refreshHeader = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewsList)];
    self.tableView.mj_header = refreshHeader;
    [self.view addSubview: self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:cell_id_dynamic bundle:nil] forCellReuseIdentifier:cell_id_dynamic];
    [self.tableView registerNib:[UINib nibWithNibName:cell_id_market bundle:nil] forCellReuseIdentifier:cell_id_market];
    [self.tableView registerNib:[UINib nibWithNibName:cell_id_release bundle:nil] forCellReuseIdentifier:cell_id_release];
    [self.tableView registerNib:[UINib nibWithNibName:cell_id_purchase bundle:nil] forCellReuseIdentifier:cell_id_purchase];
}

- (void) createSearchTitle {
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(16, 0, ScreenWidth - 16 - 36 - 8, 36)];
    titleView.backgroundColor = COLOR_BG;
    titleView.layer.cornerRadius = 18;
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"请输入搜索内容";
    label.font = [UIFont systemFontOfSize:14.0f];
    label.textColor = HEXRGB(0xcccccc);
    CGSize textSize = [TextUtil text2Size:label.text andMaxSize:titleView.frame.size andFont:label.font];
    CGFloat textX = (titleView.frame.size.width - textSize.width) / 2;
    CGFloat textY = (titleView.frame.size.height - textSize.height) / 2;
    label.frame = CGRectMake(textX, textY, textSize.width, textSize.height);
    [titleView addSubview:label];
    
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search"]];
    image.frame = CGRectMake(label.frame.origin.x - 18, label.frame.origin.y, 14, 14);
    [titleView addSubview:image];
    
    self.navigationItem.titleView = titleView;
}

#pragma mark - tableViewDataSource
//返回tableView有几组数据，默认返回1，可以不要重写
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

//返回tableView每组有几条数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (3 == section) {
        //这里表示是面料现货和面料采购
        return 2;
    }
    return 1;
}

//返回tableView的每一行的Item，在这里写，这个方法是重点
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0 == indexPath.section) {
        //纺织动态
        DynamicViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id_dynamic forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.titleTv.text = @"纺织动态";
        cell.countTv.text = @"1732";
        cell.count1Tv.text = @"2356";
        cell.count2Tv.text = @"888";
        return cell;
    } else if (1 == indexPath.section) {
        //纺织市场
        MarketViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id_market forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.onlineCountTv.text = @"23";
        cell.totalCountTv.text = @"10000";
        NSMutableArray *list = [NSMutableArray array];
        for (int i = 0; i < 10; i++) {
            GroupBean *bean = [[GroupBean alloc] initWith:@"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1902934933,556456904&fm=26&gp=0.jpg" andName:[NSString stringWithFormat:@"item -> %d", i]];
            [list addObject:bean];
        }
        cell.groups = list;
        return cell;
    } else if (2 == indexPath.section) {
        //两个发布按钮
        ReleaseViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id_release forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        WeakSelf
        cell.go2ReleaseBlock = ^(int type) {
//            [self getNewsList];
//
//            SourceDetailViewController *sourceDetailVC = [[SourceDetailViewController alloc] init];
//            sourceDetailVC.sourceId = @"1";
//            sourceDetailVC.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:sourceDetailVC animated:YES];
            [weakSelf go2Album];
        };
        return cell;
    } else if (3 == indexPath.section) {
        //面料现货和面料采购
        
        NSMutableArray *list = [NSMutableArray array];
        for (int i = 0; i < 3; i++) {
            SourceBean *bean = [[SourceBean alloc] init];
            bean.imgUrl = @"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2071531164,3511861342&fm=26&gp=0.jpg";
            bean.title = [NSString stringWithFormat:@"item -> %d", i];
            [list addObject:bean];
        }
        
        PurchaseViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id_purchase forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 0) {
            cell.sourceIconIv.image = [UIImage imageNamed:@"homeTitle_面料现货"];
            cell.sourceTitleTv.text = @"面料现货";
            cell.dayCountTv.text = @"今日现货";
            cell.dayCount.text = @"12";
            cell.totalTv.text = @"累计现货";
            cell.totalCountTv.text = @"536";
        } else {
            cell.sourceIconIv.image = [UIImage imageNamed:@"homeTitle_面料采购"];
            cell.sourceTitleTv.text = @"面料采购";
            cell.dayCountTv.text = @"今日采购";
            cell.dayCount.text = @"15";
            cell.totalTv.text = @"累计采购";
            cell.totalCountTv.text = @"682";
        }
        
        cell.sources = list;
        return cell;
    }
    return nil;
}

//返回tableView每一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = indexPath.section;
    if (0 == section) {
        return 100;
    } else if (1 == section) {
        return 300;
    } else if (2 == section) {
        return 65;
    } else if (3 == section) {
        return 200;
    }
    return 100;
}

#pragma mark -

#pragma mark - tableViewDelegate
//点击事件可以在这里实现
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark -

#pragma mark - 网络请求
//MARK: 获取新闻列表
- (void) getNewsList {
    NSDictionary *params = @{
        @"token":@"dbac58fd32dbd5743a12",
        @"appid":@"123",
        @"phone":@"15700064864",
        @"user_pass":@"123456",
        @"jpush_type":@"2",
        @"landing":@"安卓"
    };
    WeakSelf;
    [[AFNetworkHelper defaultAFNetwork] getNewsList:params andSucucess:^(id data) {
        NSLog(@"success : %@", data);
        [weakSelf.tableView.mj_header endRefreshing];
    } andError:^(NSString *msg, NSInteger code) {
        NSLog(@"error msg = %@, code = %lu", msg, code);
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    return;
}

#pragma mark -

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void) go2Camera {
    UIImagePickerController *controller = [[UIImagePickerController alloc] init];
    controller.sourceType = UIImagePickerControllerSourceTypeCamera;
    controller.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    controller.allowsEditing = YES;
    [self presentViewController:controller animated:YES completion:^{
        
    }];
}

- (void) go2Album {
    UIImagePickerController *controller = [[UIImagePickerController alloc] init];
    controller.view.backgroundColor = [UIColor whiteColor];
    controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    controller.allowsEditing = YES;
    [self presentViewController:controller animated:YES completion:nil];
    
}

@end
